package org.itboys.mobile.service.mysql.mobile.promotion;

import org.itboys.commons.utils.time.TimeUtils;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dao.promotion.SundayCouponMapper;
import org.itboys.mobile.dto.promotion.SundayCouponDto;
import org.itboys.mobile.entity.mysql.promotion.SundayCoupon;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.*;

/**
 * 作者：zzq
 * 时间：2017年5月15日
 * 描述：圣代_促销管理_移动_service。含多种促销。当前只有优惠券促销
 */
@Service
public class SundayMobilePromotionService {

    @Autowired
    private SundayCouponMapper couponMapper;

    /****************************优惠券部分*******************/


    /**
     * 获取用户的优惠券
     * @param request
     * @return
     */
    public List<SundayCouponDto> getMemberCoupon(HttpServletRequest request, Long memberId, Integer status, BigDecimal amount){
            Map<String,Object> param= ParamUtil.packageMysqlPageParam(request,"");
            List<Integer> statues =new ArrayList<Integer>();
            //0就查询全部状态
            if(status==0){
                statues.add(SundayCommonConstants.counpon_status_wsy);
                statues.add(SundayCommonConstants.counpon_status_ysy);
                statues.add(SundayCommonConstants.counpon_status_ygq);
            }else{
                statues.add(status);
            }

            //传入金额了就查询未使用的
            if(amount!=null){
                statues.clear();
                statues.add(SundayCommonConstants.counpon_status_wsy);
                param.put("baseAmount_smaller",amount);
            }
            param.put("statuses",statues);
            param.put("memberId",memberId);
            return packageDtoMuilt(couponMapper.select(param));
    }

    /**
     *组装DTO
      * @param coupons
     */
    public List<SundayCouponDto> packageDtoMuilt(List<SundayCoupon> coupons){
        List<SundayCouponDto> dtos = new ArrayList<SundayCouponDto>();
        for(SundayCoupon coupon:coupons){
            SundayCouponDto dto=new SundayCouponDto(coupon.getId(), coupon.getCreateTime(), coupon.getCreateDate(),
                    coupon.getBaseAmount().setScale(2,BigDecimal.ROUND_HALF_UP).toString(),coupon.getDecuAmount().setScale(2,BigDecimal.ROUND_HALF_UP).toString() ,coupon.getStatus(),
                    coupon.getSource(), TimeUtils.format(coupon.getEndTime(),"yyyy-MM-dd"));
            dtos.add(dto);
        }
        return  dtos;

    }

    /****************************优惠券部分结束*******************/
}
